package com.um.jdy.app.activity.manager;


import com.um.jdy.app.activity.entity.dto.CouponsGoodsPageDTO;
import com.um.jdy.app.activity.entity.dto.MyCouponsPageDTO;
import com.um.jdy.app.activity.entity.vo.*;
import com.um.jdy.app.shop.entity.dto.GoodsPageDTO;
import com.um.jdy.app.shop.entity.vo.GoodsItemVO;
import com.um.jdy.common.activity.code.CouponsCode;
import com.um.jdy.common.activity.entity.po.Coupons;
import com.um.jdy.common.activity.service.CouponsService;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.entity.vo.GoodsVO;
import com.um.jdy.common.shop.enums.CouponsEnum;
import com.um.jdy.common.shop.service.GoodsService;
import com.um.springboot.starter.config.SystemSQL;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.condition.ConditionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class CouponsManager {

    @Autowired
    CouponsService couponsService;

    @Autowired
    MemberUserService memberUserService;

    @Autowired
    GoodsService goodsService;



    /**
     * 领取优惠券
     * @param activityCouponsId
     * @return
     */
    public void receive(String activityCouponsId){
        couponsService.receive(activityCouponsId, memberUserService.get().getMemberUserId());
    }

    /**
     * 兑换优惠券
     * @param code
     * @return
     */
    public void exchange(String code){
        couponsService.exchange(code, memberUserService.get().getMemberUserId());
    }

    /**
     * 查询商品可用的优惠券列表
     *
     * @param shopGoodsId
     * @return
     */
    public List<GoodsCouponsVO> getCouponsListByGoods(String shopGoodsId) {
        String userId = memberUserService.get().getMemberUserId();
        List<GoodsCouponsVO> list = couponsService.getViewModelList("get_coupons_list_by_goods", GoodsCouponsVO.class,
                new Object[]{userId,memberUserService.getComId(),shopGoodsId,userId,memberUserService.getComId()});
        for(GoodsCouponsVO item : list){
            if(CouponsEnum.Type.Gift.name().equals(item.getType())){//赠品券读取商品列表
                List goodsList = couponsService.getViewModelList("get_coupons_goods_list", GoodsVO.class,
                        new Object[]{item.getActivityCouponsId(),item.getComId()});
                item.setGoods(goodsList);
            }
        }
        return list;
    }


}
